In this demo, we present a basic demonstration of our project by training the qGAN to generate an 8 $\times$ 8 pixel art of smiley face. Enjoy :)
First, install python modules as following:
import numpy as np
import plotly.io as pio
pio.renderers.default = "notebook"
Second, import our self-written module in the --imgen.py--
from src.imgen import ImageGenerator
Now, initialze the input pixel art.
NUM_QUBITS = 6
NUM_LAYERS = 2
EPOCH_SAMPLE_SIZE = 10**4
BATCH_SAMPLE_SIZE = 10**3
pixel_art = np.array([
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 1, 0],
[0, 1, 0, 0, 0, 0, 1, 0],
[0, 0, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]
], dtype=np.float)
Initialize the qGAN class for image recognition
imgen = ImageGenerator(
NUM_QUBITS, NUM_LAYERS,
epoch_sample_size=EPOCH_SAMPLE_SIZE, batch_sample_size=BATCH_SAMPLE_SIZE,
enable_remapping=True
)
Load the pixel art. After preprocessing with Gaussian filters, this is what your original art looks like!
imgen.load_image(pixel_art, blur_sigma=0.6, show_figure=True)
Train the qGAN and see how the output of the generator changes in 300 epochs! (Spoiler alert: you can see your smiley face again after only ~30 epochs!)
NUM_EPOCHS = 300
imgen.train(imgen.make_dataset(), NUM_EPOCHS, show_progress=True)
Training epoch 300 of 300:
Generate interactive probability distribution output with training epoch
imgen.plot_output_distribution_history()
Plot loss function versus steps. A trend towards convergence can be seen.
imgen.plot_loss_history()
Plot the final averaged distribution output. It matches pretty well with the original pixel face!
imgen.plot_final_output_distribution(avg_window=5)
Get the final averaged variational parameters for the circuit
imgen.get_final_params(avg_window=10)
array([[[-0.97407997, -0.51002026, -0.21671724, -0.19944257,
0.05155891, -0.04443405],
[-0.09982817, -0.07074447, 0.22649863, -0.14553228,
0.01164286, -0.06510032]]], dtype=float32)